package com.qzsoft.zhjd.server.modules.laboratory.service.impl;

import java.util.Date;

import com.qzsoft.zhjd.server.handler.BizException;
import com.qzsoft.zhjd.server.modules.laboratory.entity.CollectLaboratoryDevice1Entity;
import com.qzsoft.zhjd.server.modules.laboratory.entity.CollectLaboratoryDuctility1Entity;
import com.qzsoft.zhjd.server.modules.laboratory.entity.CollectLaboratoryFurnaceEntity;
import com.qzsoft.zhjd.server.modules.laboratory.mapper.CollectLaboratoryDuctilityMapper;
import com.qzsoft.zhjd.server.modules.laboratory.mapper.CollectLaboratoryFurnaceMapper;
import com.qzsoft.zhjd.server.modules.laboratory.service.CollectLaboratoryDevice1Service;
import com.qzsoft.zhjd.server.modules.laboratory.service.CollectLaboratoryLudongService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;

import cn.hutool.core.lang.UUID;
import lombok.extern.slf4j.Slf4j;

/**
 * @ClassName LaboratoryLudongServiceImpl
 * @Description 鲁东实验室数据接口实现
 * @author linhuawei
 * @since 2022-08-30
 */
@Slf4j
@Service
public class CollectLaboratoryLudongServiceImpl implements CollectLaboratoryLudongService {

    @Autowired
    private CollectLaboratoryDuctilityMapper collectLaboratoryDuctilityMapper;

    @Autowired
    private CollectLaboratoryFurnaceMapper collectLaboratoryFurnaceMapper;

    @Autowired
    private CollectLaboratoryDevice1Service collectLaboratoryDevice1Service;

    @Override
    public void receive(JSONObject json) {
        // 判断试验类型
        String testType = json.getString("testType");
        if ("YD".equals(testType)) {
            saveDuctility(json);
        } else if ("RSL".equals(testType)) {
            saveFurnace(json);
        } else {
            // 未知的试验类型
            log.info("试验类型不存在：{}", json);
            throw new BizException("TASKTYPE_ERROR");
        }
    }

    /**
     * @Title saveDuctility
     * @Description 添加延度数据
     * @param entity
     * @return
     */
    private void saveDuctility(JSONObject json) {
        // 格式化数据
        CollectLaboratoryDuctility1Entity entity = null;
        try {
            entity = json.toJavaObject(CollectLaboratoryDuctility1Entity.class);
        } catch (Exception e) {
            log.info("延度格式化错误:{}", json);
            throw new BizException("JSON_COVER_ERROR");
        }

        // 判断设备是否存在
        CollectLaboratoryDevice1Entity device = collectLaboratoryDevice1Service.getByCode(entity.getDeviceCode());
        if (null == device) {
            log.info("延度设备不存在:{}", json);
            throw new BizException("DEVICE_NOT_FOUND");
        }

        // TODO 判断编号是否重复

        // 执行添加操作
        entity.setId(UUID.randomUUID().toString());
        entity.setBaseCode(device.getBaseCode());
        entity.setInsertTime(new Date());
        collectLaboratoryDuctilityMapper.insert(entity);
    }

    /**
     * @Title saveFurnace
     * @Description 保存燃烧炉数据
     * @param json 燃烧炉数据
     */
    private void saveFurnace(JSONObject json) {
        // 格式化数据
        CollectLaboratoryFurnaceEntity entity = null;
        try {
            entity = json.toJavaObject(CollectLaboratoryFurnaceEntity.class);
        } catch (Exception e) {
            log.info("燃烧炉格式化错误:{}", json);
            throw new BizException("JSON_COVER_ERROR");
        }

        // 判断设备是否存在
        CollectLaboratoryDevice1Entity device = collectLaboratoryDevice1Service.getByCode(entity.getDeviceCode());
        if (null == device) {
            log.info("燃烧炉设备不存在:{}", json);
            throw new BizException("DEVICE_NOT_FOUND");
        }

        // TODO 判断编号是否重复

        // 执行添加操作
        entity.setId(UUID.randomUUID().toString());
        entity.setBaseCode(device.getBaseCode());
        entity.setInsertTime(new Date());
        collectLaboratoryFurnaceMapper.insert(entity);
    }
}
